using FileServer.Abstractions.Models.Entities;
using Microsoft.Extensions.Options;
using SqlSugar;

namespace FileServer.Storage.MySql;

/// <summary>
/// sql sugar context
/// </summary>
public class MySqlSugarContext
{
    public readonly SqlSugarScope Scope;
    private readonly IOptions<MySqlOptions> _options;
    public MySqlSugarContext(IOptions<MySqlOptions> options)
    {
        _options = options;
        SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
        {
            DbType = DbType.MySql,
            ConnectionString = _options.Value.ConnectionString,
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute,
            ConfigureExternalServices = new ConfigureExternalServices()
            {
                EntityService = (s, p) =>
                {
                    p.IfTable<FileResource>()
                        .UpdateProperty(it => it.Id, it =>
                        {
                            it.IsPrimarykey = true;
                            it.DataType = "varchar(50)";
                            it.ColumnDescription = "文件标识";
                        })
                        .UpdateProperty(it => it.FileNo, it =>
                        {
                            it.DataType = "varchar(500)";
                            it.ColumnDescription = "文件号";
                        })
                        .UpdateProperty(it => it.Length, it =>
                        {
                            it.DataType = "int";
                            it.ColumnDescription = "文件大小";
                        })
                        .UpdateProperty(it => it.Hash, it =>
                        {
                            it.DataType = "varchar(1000)";
                            it.ColumnDescription = "文件hash";
                            it.IsNullable = true;
                        })
                        .UpdateProperty(it => it.Path, it =>
                        {
                            it.DataType = "varchar(500)";
                            it.ColumnDescription = "完整存储路径";
                            it.IndexGroupNameList = new string[] { "path_index" };
                        })
                        .UpdateProperty(it => it.OriginName, it =>
                        {
                            it.DataType = "varchar(500)";
                            it.ColumnDescription = "原文件名";
                        })
                        .UpdateProperty(it => it.IsTemp, it =>
                        {
                            it.DataType = "bit";
                            it.ColumnDescription = "是否是临时文件";
                        })
                        .UpdateProperty(it => it.PeriodMinute, it =>
                        {
                            it.DataType = "int";
                            it.ColumnDescription = "有效时长 分钟数";
                        })
                        .UpdateProperty(it => it.StorageKey, it =>
                        {
                            it.DataType = "varchar(50)";
                            it.ColumnDescription = "存储来源";
                        })
                        .UpdateProperty(it => it.ProviderType, it =>
                        {
                            it.DataType = "varchar(100)";
                            it.ColumnDescription = "上传服务提供者 local 本地  oss 阿里云oss 其他的可自行扩充";
                            it.IsNullable = true;
                        })
                         .UpdateProperty(it => it.CreateTime, it =>
                         {
                             it.DataType = "datetime";
                             it.ColumnDescription = "创建时间";
                         });

                }
            }
        },
            db =>
            {
                if (options.Value.OpenLogger)
                {
                    db.Aop.OnLogExecuting = (sql, param) =>
                    {
                        //打印sql 日志
                        Console.WriteLine("[FileServer.Mysql执行]" + sql);
                    };
                }
            }
        );
        Scope = sqlSugar;

        Scope.CodeFirst.As<FileResource>($"{(string.IsNullOrEmpty(_options.Value.DefaultTablePrefix) ? "" : _options.Value.DefaultTablePrefix + "_")}FileResource").InitTables<FileResource>();


    }
}